//
//  UIImage+WJResize.m
//  WJBSApp
//
//  Created by dllo on 16/7/16.
//  Copyright © 2016年 王景伟. All rights reserved.
//

#import "UIImage+WJResize.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@implementation UIImage (WJResize)

//图片剪切
- (UIImage*)cutImageWithRadius:(int)radius
{
    UIGraphicsBeginImageContext(self.size);
    CGContextRef gc = UIGraphicsGetCurrentContext();
    
    float x1 = 0.;
    float y1 = 0.;
    float x2 = x1+self.size.width;
    float y2 = y1;
    float x3 = x2;
    float y3 = y1+self.size.height;
    float x4 = x1;
    float y4 = y3;
    radius = radius*2;
    
    CGContextMoveToPoint(gc, x1, y1+radius);
    CGContextAddArcToPoint(gc, x1, y1, x1+radius, y1, radius);
    CGContextAddArcToPoint(gc, x2, y2, x2, y2+radius, radius);
    CGContextAddArcToPoint(gc, x3, y3, x3-radius, y3, radius);
    CGContextAddArcToPoint(gc, x4, y4, x4, y4-radius, radius);
    
    
    CGContextClosePath(gc);
    CGContextClip(gc);
    
    CGContextTranslateCTM(gc, 0, self.size.height);
    CGContextScaleCTM(gc, 1, -1);
    CGContextDrawImage(gc, CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage);
    
    
    
    UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newimage;
}


//+ (UIImage *)getVideoThumbnailWithFilePath:(NSString *)filePath
//{
//    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
//    moviePlayer.shouldAutoplay = NO;
//    UIImage *image = [moviePlayer thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
//    return nil;
//}


+ (UIImage *)imageWithVideo:(NSURL *)videoUrl
{
    // 根据视频的URL创建AVURLAsset
    AVURLAsset *asset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];
    
    // 根据AVURLAsset创建AVAssetImageGenerator
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    
    // 定义获取0帧出的视频截图
    CMTime time = CMTimeMake(27, 10);
    NSError *error = nil;
    CMTime actualTime;
    
    // 获取time处的视频截图
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];

    // 将CGImageRef 转化为UIImage
    
    UIImage *thumb = [[UIImage alloc]initWithCGImage:image];
    CGImageRelease(image);
    return thumb;

}












@end
